package com.fwzc.giuseppe.activity;

import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import com.fwzc.giuseppe.R;
import com.fwzc.giuseppe.Api.Api;
import com.fwzc.giuseppe.base.BaseActivity;
import com.fwzc.giuseppe.dao.UserKeyDao;
import com.fwzc.giuseppe.entity.UserKey;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

public class AddUserActivity extends BaseActivity implements OnClickListener{
	private TextView forgetPw, login, register;
	private EditText username, password;
	private String name, pw;
	private HttpUtils httpUtil;
	private ProgressDialog load;
	private UserKeyDao dao;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_login);

		initBar();
		initView();
		initData();
	}

	private void initView() {
		dao = new UserKeyDao(this);
		httpUtil = new HttpUtils();
		load = new ProgressDialog(this);
		load.setMessage(getResources().getString(R.string.deal));
		load.setCancelable(false);
		
		forgetPw = (TextView) findViewById(R.id.for_pw);
		login = (TextView) findViewById(R.id.login_btn);
		register = (TextView) findViewById(R.id.register_btn);
		username = (EditText) findViewById(R.id.log_username);
		password = (EditText) findViewById(R.id.log_password);
	}

	private void initData() {
		actionbar_side_name.setText("登录");
		iv_actionbar_side_left.setVisibility(View.VISIBLE);
		forgetPw.setOnClickListener(this);
		login.setOnClickListener(this);
		register.setOnClickListener(this);
		iv_actionbar_side_left.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		if (v == forgetPw) {
			Intent fpwIntent = new Intent(this, FindPwActivity.class);
			startActivity(fpwIntent);
		} else if (v == login) {
			name = username.getText().toString().trim();
			pw = password.getText().toString().trim();
			// 控制显示或隐藏输入法面板的类
			InputMethodManager imm = (InputMethodManager) this
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(password.getWindowToken(), 0);
			if (name.equals("") || pw.equals("")) {
				showToast("请输入账号或密码！");
				return;
			} else {
				sendRequest();
			}

		} else if (v == register) {
			Intent registerIntent = new Intent(this, RegisterActivity.class);
			startActivity(registerIntent);
		} else if (v == iv_actionbar_side_left) {
			finish();
		}

	}

	/**
	 * 登录请求
	 */
	private void sendRequest() {
		RequestParams params = new RequestParams("UTF-8");
		params.addBodyParameter("userName", username.getText().toString());
		params.addBodyParameter("password", password.getText().toString());

		httpUtil.send(HttpRequest.HttpMethod.POST, Api.Login, params,
				new RequestCallBack<String>() {

					@Override
					public void onStart() {
						load.show();
					}

					@Override
					public void onFailure(HttpException arg0, String response) {
						load.dismiss();
						showToast("登录失败");
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseStr) {
						try {
							JSONObject response = new JSONObject(responseStr.result);
							int resultCode = response.getInt("resultCode");
							if (resultCode == 0) {
								load.dismiss();
								JSONObject message = response.getJSONObject("dataList");
								UserKey userKey = new UserKey();
								userKey.userId = response.getString("userId");
								userKey.userIdentityNumber = message.getString("userIdentityNumber");
								userKey.name = "未知";
								userKey.headPortrait = "11111";
								dao.addUserKey(userKey);
								Intent intent = new Intent();
								intent.putExtra("userId", response.getString("userId"));
								intent.putExtra("userIdentityNumber", message.getString("userIdentityNumber"));
								intent.putExtra("name", userKey.name);
								intent.putExtra("headPortrait", userKey.headPortrait);
								setResult(RESULT_OK,intent);
								finish();
							}else{
								load.dismiss();
								showToast(response.getString("msg"));
							}
						} catch (JSONException e) {
							load.dismiss();
							showToast("解析出错");
						}

					}
				});
	}
}
